package com.mikasa.springboot.example.controller;

import com.mikasa.springboot.example.domain.elasticsearch.Post;
import com.mikasa.springboot.example.elasticsearch.BookService;
import com.mikasa.springboot.example.elasticsearch.PostService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sherlock on 16/9/21.
 */

@RestController
@RequestMapping("/elasticsearch")
public class ElasticsearchController {

    Logger log = LoggerFactory.getLogger(ElasticsearchController.class);

    @Autowired
    private PostService postService;

    @Autowired
    private BookService bookService;

    @ApiOperation(value="测试保存elasticsearch", notes="elasticsearch保存Post")
    @RequestMapping(value = "/save",method = RequestMethod.GET)
    public Object save() {
        log.info("测试elasticsearch...");

        Post post1 = new Post();
        post1.setId("1");
        post1.setTitle("测试elasticsearch");
        post1.setRating(9);
        post1.setTag("阿迪达斯篮球鞋");
        Post post2 = new Post();
        post2.setId("2");
        post2.setTitle("测试elasticsearch");
        post2.setRating(10);
        post2.setTag("耐克篮球鞋");
        postService.save(post1);
        postService.save(post2);
        return "success";
    }

    @ApiOperation(value="测试获取elasticsearch", notes="elasticsearch获取list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Object list() {
        log.info("测试获取elasticsearch...");
        Iterable<Post> iterable = postService.findAll();
        return iterable;
    }

    @ApiOperation(value="通过id获取post", notes="elasticsearch通过id获取post")
    @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path", dataType = "String")
    @RequestMapping(value = "/findById/{id}",method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        log.info("测试获取elasticsearch...ById");
        Post post = postService.findOne(id);
        return post;
    }

    @ApiOperation(value="通过title获取post", notes="elasticsearch通过title获取post")
    @ApiImplicitParam(name = "title", value = "title", required = true, paramType = "path", dataType = "String")
    @RequestMapping(value = "/findByTitle/{title}",method = RequestMethod.GET)
    public Object findByTitle(@PathVariable String title) {
        log.info("测试获取elasticsearch...findByTitle");
        Map<String,Object> map = new HashMap<String,Object>();
        List<Post> post = postService.findByTitle(title);
        map.put("post",post);
        return map;
    }

    @ApiOperation(value="通过tagName获取post", notes="elasticsearch通过tagName获取post")
    @ApiImplicitParam(name = "tagName", value = "tagName", required = true, paramType = "path", dataType = "String")
    @RequestMapping(value = "/findByTagName/{tagName}",method = RequestMethod.GET)
    public Object findByTagsName(@PathVariable String tagName) {
        log.info("测试获取elasticsearch...findByTagsName");
        List<Post> page = postService.findByTagName(tagName);
        return page;
    }

    @ApiOperation(value="通过rating获取post", notes="elasticsearch通过rating获取post")
    @RequestMapping(value = "/findByRatingBetween",method = RequestMethod.POST)
    public Object findByRatingBetween(@RequestParam(value = "beginning",required = false) Integer beginning,
                                      @RequestParam(value = "end",required = false) Integer end) {
        log.info("测试获取elasticsearch...findByRatingBetween");
        List<Post> postList = postService.findByRatingBetween(beginning,end);
        return postList;
    }
}
